import java.util.ArrayList;
import java.util.ListIterator;

/**
 * 
 * @author Devin Wolanick
 * @Version 1.0
 * 
 * EvaluateHand evaluates given hand and returns a list of results
 */
public class EvaluateHand {
	
	/**
	 * Variables
	 */
	private ArrayList<Card> myHand;
	
	/**
	 * Constructor
	 * @param myHand Hand to evaluate
	 */
	public EvaluateHand (ArrayList<Card> myHand) {
		this.myHand = myHand;
	}
	
	/**
	 * Adds Values to all possible results
	 * @param myResults Current possible Results
	 * @param toAdd Value to Add
	 */
	private void resultsAdd(ArrayList<Integer> myResults, int toAdd) {
		for (int i=0; i<myResults.size(); i++)
			myResults.set(i, myResults.get(i)+toAdd);
	}
	
	/**
	 * Deals with multiple Ace Values
	 * @param myResults Current List of possible Values
	 */
	private void addAce(ArrayList<Integer> myResults) {
		int initialSize = myResults.size();
		for (int i=0; i<initialSize; i++)
			myResults.add(myResults.get(i));
		for (int i=0; i<initialSize; i++)
			myResults.set(i, myResults.get(i)+1);
		for (int i=initialSize; i<initialSize*2; i++)
			myResults.set(i, myResults.get(i)+11);
	}
	
	/**
	 * Trims useless Values
	 * @param myResults List of useful values
	 */
	private void trim(ArrayList<Integer> myResults) {
		for (int i=0; i<myResults.size()-1; i++)
			for (int j=i+1; j<myResults.size(); j++)
				if (myResults.get(i) == myResults.get(j))
					myResults.remove(j);
	}
	
	/**
	 * Calculate Result
	 * @return List of possible Hand Values
	 */
	public ArrayList<Integer> evaluate() {
		ArrayList<Integer> myResults = new ArrayList<Integer>();
		ListIterator<Card> handIter = myHand.listIterator();
		myResults.add(0);
		while (handIter.hasNext()) {
			Card currentCard = handIter.next();
			if (currentCard.getNumber()>1 && currentCard.getNumber()<11)
				resultsAdd(myResults, currentCard.getNumber());
			else if (currentCard.getNumber()>10)
				resultsAdd(myResults, 10);
			else if (currentCard.getNumber() == 1)
				addAce(myResults);
		}
		trim(myResults);
		return myResults;
	}
}